package com.df.ez_game.tools;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadHelper {
    private static final ScheduledExecutorService scheduler = 
        Executors.newScheduledThreadPool(2);

    public static ThreadPoolExecutor getNewSingleExecutor(){
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1,
                60L, TimeUnit.SECONDS,
                new SynchronousQueue<Runnable>());
        return threadPoolExecutor;
    }

    /**
     * 创建定时任务
     * @param command 要执行的任务
     * @param initialDelay 初始延迟时间
     * @param period 执行周期
     * @param unit 时间单位
     */
    public static void scheduleAtFixedRate(
        Runnable command, 
        long initialDelay, 
        long period, 
        TimeUnit unit
    ) {
        scheduler.scheduleAtFixedRate(
            command, initialDelay, period, unit
        );
    }
}
